package com.neuedu.listener;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebListener
public class MyServletContextListener implements ServletContextListener  ,ServletContextAttributeListener{


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext application =  sce.getServletContext();
        Object source = sce.getSource();

        System.out.println("MyServletContextListener.contextInitialized");
        System.out.println("application = " + application);
        System.out.println("source = " + source);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext application =  sce.getServletContext();
        Object source = sce.getSource();

        System.out.println("MyServletContextListener.contextDestroyed");
        System.out.println("application = " + application);
        System.out.println("source = " + source);
    }


    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object value = event.getValue();

        System.out.printf("MyServletContextListener.attributeAdded       name:%s , value:%s\r\n",name,value.toString());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object value = event.getValue();

        System.out.printf("MyServletContextListener.attributeRemoved       name:%s , value:%s\r\n",name,value.toString());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        String name = event.getName();
        Object value = event.getValue();
        Object newValue = event.getServletContext().getAttribute(name);

        System.out.printf("MyServletContextListener.attributeReplaced       name:%s , value:%s    ,newValue :%s  \r\n",name,value.toString() ,newValue.toString());
    }
}
